package com.ljs.mongodemo.demo.service.impl;

import com.ljs.mongodemo.demo.bean.User;
import com.ljs.mongodemo.demo.service.MongodbUserService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class MongodbUserServiceImpl implements MongodbUserService {
  @Resource
    MongoTemplate mongoTemplate;
  //注入
    @Override
    public void saveUser(User user) {
        mongoTemplate.save(user,"user");
        // 如果文档采用类名首字符小写，则可以这样保存
        // mongoTemplate.save(user);

    }

    @Override
    public DeleteResult deleteUser(String id) {
        //确定要删除的对象
        Criteria criteriaupd=Criteria.where("id").is(id);
        Query q=new Query(criteriaupd);
        //删除
        DeleteResult deleteResult=mongoTemplate.remove(q,User.class);
        return deleteResult;
    }

    @Override
    public List<User> findUser(String userName, String note, int curpage, int limit) {
       Criteria criteria=Criteria.where(userName).regex(userName).and("note").regex(note);//设为模糊查询
        Query query=new Query().limit(limit).skip((curpage-1)*limit);//分页查询
        List<User> users=     mongoTemplate.find(query,User.class);
        return users;
    }

    @Override
    public UpdateResult updateUser(User user) {
        //确定要更新的对象
        Criteria criteriaupd=Criteria.where("id").is(user.getId());
        Query q=new Query(criteriaupd);
        //Update update= Update.update("userName",userName);//将userName排除在外
        Update update = new Update();
        update.set("user_name",user.getUserName());
        update.set("note",user.getNote());
        update.set("create_time",user.getCreateTime());
        UpdateResult updateResult=mongoTemplate.updateFirst(q,update,User.class);
        // 更新多个对象
        // UpdateResult result = mongoTemplate.updateMulti(query, update, User.class);
        return updateResult;
    }

    @Override
    public User getUser(String id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String datestr=sdf.format(date);
        Calendar calendar=Calendar.getInstance();
        //月份
    int hour=calendar.get(Calendar.HOUR);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        Criteria criteriaID=Criteria.where("id").is(id);//条件
        Query query=new Query(criteriaID);//将条件加入
        User user=    mongoTemplate.findOne(query,User.class);
        //也可以这样
        //return mongoTemplate.findById(id, User.class);
        return user;
    }

    @Override
    public User getUserByname(String userName) {
        Criteria criteria=Criteria.where("user_name").is(userName);
        Query query=new Query(criteria);
        User user=    mongoTemplate.findOne(query,User.class);
        return user;
    }

    @Override
    public List<User> findall(int curpage, int limit) {
       //分页
        Query pagequery=new Query().skip((curpage-1)*limit).limit(limit);
       //排序
       List list=new ArrayList();
        list.add("id");
        //根据id排序升序 ASC ,降序DESC
        Sort sort1 = Sort.by(Sort.Direction.ASC,"id");

       // Sort sort1 = Sort.by(Sort.Direction.ASC,"id").and(Sort.by(Sort.Direction.ASC, "user_Name"));//多条件DEVID、time
        List<User>  userList=  mongoTemplate.find(pagequery.with(sort1),User.class);

       Long count=mongoTemplate.count(pagequery.with(sort1),User.class);
        System.out.println("总数是"+count);
        return userList;
    }

    @Override
    public AggregationResults aggregation(int pageNum, int pageSize, Criteria criteria1, Criteria criteria2, String field1, String field2, String sortName, Sort sort, String collectionName) {
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(criteria1),
                Aggregation.match(criteria2),
                Aggregation.group(field1,field2).last(sortName).as(sortName),
                Aggregation.sort(sort),
                Aggregation.limit(pageSize),
                Aggregation.skip((pageNum-1)*pageSize)
        );


        AggregationResults aggregationResults=mongoTemplate.aggregate(aggregation,collectionName,User.class);
        return aggregationResults;
    }

//    @Override
//    public List<String> selbyType(String type) {
//        Criteria criteria=Criteria.where("type").is(type);
//        Query query=new Query(criteria);
//        List<String>  userList=   mongoTemplate.find(query,String.class);
//        return userList;
//    }

}
